/* eslint-disable default-case */
import axios from "axios"
import { message } from "antd"

//产生一个新的axios
const newAxios = axios.create({
    baseURL: "http://xawn.f3322.net:8012",
    timeout: 5000
})

newAxios.interceptors.request.use((request) => {
    if (localStorage.token) {
        request.headers.Authentication = localStorage.token
    }
    return request
}, (error) => {
    return Promise.reject("请求失败")
})

newAxios.interceptors.response.use((resp) => {
    //将对象进行处理
    return resp
}, (error) => {
    //说明状态码 400以上 500以上
    const response = error.response
    switch(response.status){
        case 500:
            message.error("服务器正在休息！！！")
            break;
        case 401:
            message.error("身份过期，请重新认证")
            localStorage.removeItem("token")
            localStorage.removeItem("userInfo")
            window.location.assign("/")
            break;
        case 404:
            message.error("资源不存在，检查路径")
            break;
        
    }
   
    return Promise.reject(error)
})
export default newAxios